<?php

namespace App\Http\Controllers\Admin;

use App\Model\Cate;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class CateController extends Controller
{
    /**
     * Display a listing of the resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function index(Request $request)
    {
        $cate = (new Cate())->tree();
        return view("admin.cate.list",compact('cate'));
    }

    /**
     * Show the form for creating a new resource.
     *
     * @return \Illuminate\Http\Response
     */
    public function create()
    {
        //获取一级类
        $cate = Cate::where("cate_pid",0)->get();
        return view("admin.cate.add",compact("cate"));
    }

    /**
     * Store a newly created resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @return \Illuminate\Http\Response
     */
    public function store(Request $request)
    {
        //新增
        $res = Cate::create($request->except("_token"));

        if($res){
            $data = [
                'status' => 0,
                'message'=> '新增成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '新增失败！'
            ];
        }
        //json_decode();
        return $data;
    }

    /**
     * Display the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function show($id)
    {
        //
    }

    /**
     * Show the form for editing the specified resource.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function edit($id)
    {
        //
        $cate = Cate::find($id);
        return view("admin.cate.edit",compact("cate"));
    }

    /**
     * Update the specified resource in storage.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function update(Request $request, $id)
    {
        //1.根据id修改值
        $res = Cate::where("cate_id",$request->input("cate_id"))->update($request->except("cate_id"));

        if($res){
            $data = [
                'status' => 0,
                'message'=> '修改成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '修改失败！'
            ];
        }
        //json_decode();
        return $data;
    }

    /**
     * Remove the specified resource from storage.
     *
     * @param  int  $id
     * @return \Illuminate\Http\Response
     */
    public function destroy($id)
    {
        $per = Cate::find($id);
        $res = $per->delete();
        if($res){
            $data = [
                'status' => 0,
                'message' => '删除成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message' => '删除失败！'
            ];
        }
        return $data;
    }

    //修改排序
    public function changeOrder(Request $request){
        $res = Cate::where('cate_id',$request->input("cate_id"))->update(['cate_order'=>$request->input("cate_order")]);
        if($res){
            $data = [
                'status' => 0,
                'message'=> '修改成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '修改失败！'
            ];
        }
        //json_decode();
        return $data;
    }

    //分类多删除
    public function delAll(Request $request){
        $input = $request->input("ids");

        $res = Cate::destroy($input);
        if($res){
            $data = [
                'status' => 0,
                'message'=> '删除成功！'
            ];
        }else{
            $data = [
                'status' => 1,
                'message'=> '删除失败！'
            ];
        }
        //json_decode();
        return $data;
    }
}
